package com.zhao.customer.controller;

import com.zhao.customer.dao.UserDao;
import com.zhao.customer.entity.User;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@WebServlet("/fenye.do")
public class FenController extends HttpServlet {
    private final UserDao ud = new UserDao();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String page = req.getParameter("page");//第几页
        String rows = req.getParameter("rows");//一页多少条
        System.out.println(page);
        System.out.println(rows);

        /*List ll = new LinkedList();
        ll.add(new User("zhang", "123"));
        ll.add(new User("zhao", "123"));
        ll.add(new User("li", "123"));
        ll.add(new User("li1", "123"));
        ll.add(new User("li2", "123"));
        ll.add(new User("li3", "123"));*/
        List<User> ll = null;
        try {
            ll = ud.selectByPage(Integer.parseInt(page), Integer.parseInt(rows));
        } catch (Exception e) {
            e.printStackTrace();
        }
        //new UserDao().
        Map<String, Object> map = new HashMap<>();
        try {
            map.put("total", ud.count());
        } catch (Exception e) {
            e.printStackTrace();
        }
        map.put("rows", ll);
        PrintWriter out = resp.getWriter();
        out.write(JSONObject.fromObject(map).toString());
        out.flush();
        out.close();
    }
}
